<template>
  <div id="invitedActivity">
    <h1 class="con-right-title">活动列表</h1>
    <topSearch :form-inline="formInline" @toSearch="toSearch"></topSearch>
    <!-- 操作按钮部分 -->
    <div class="table-conBtn">
      <el-button type="warning" icon="el-icon-plus" @click="addActives('add')">
        添加活动
      </el-button>
      <freshCom @searchList="searchList"></freshCom>
    </div>

    <!-- 表格部分 -->
    <el-table :data="tableData" stripe max-height="490" ref="table">
      <el-table-column type="index" label="序号" width="50" align="center">
        <template slot-scope="scope">
          {{
            scope.$index
              | filterPage(searchParams.pageNum, searchParams.pageSize)
          }}
        </template>
      </el-table-column>
      <el-table-column
        prop="activityInfo.name"
        label="活动名称"
        min-width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="createdTime"
        label="创建时间"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="activityInfo.collegeId"
        label="学院"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="activityInfo.specificActivityType"
        label="活动类别"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="changeUserDto.userName"
        label="操作人"
        min-width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column label="状态" min-width="100" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.state == 'NOT_STARTED'">未开始</span>
          <span v-if="scope.row.state == 'ONGOING'">进行中</span>
          <span v-if="scope.row.state == 'OVER'">已结束</span>
          <span v-if="scope.row.state == 'DELETE'">已删除</span>
        </template>
      </el-table-column>
      <el-table-column label="活动时间" min-width="150" align="center">
        <template slot-scope="scope">
          <div class="activity-time__wrap">
            <span>{{ scope.row.activityInfo.startTime }}</span>
            <span>{{ scope.row.activityInfo.endTime }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="活动海报" min-width="150" align="center">
        <template slot-scope="scope">
          <listImgString
            :imgInfo="scope.row.inviteActivityConfig.poster"
          ></listImgString>
        </template>
      </el-table-column>
      <el-table-column
        prop="prizeCharacters"
        label="奖品"
        min-width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="action"
        label="操作"
        min-width="200"
        align="center"
      >
        <template slot-scope="scope">
          <div class="action-wrap">
            <span
              class="action-wrap__btn"
              v-if="scope.row.state == 'NOT_STARTED'"
              @click="complie(scope.row)"
              >编辑</span
            >
            <span
              class="action-wrap__btn"
              v-if="scope.row.state == 'NOT_STARTED'"
              @click="handleDeleteActive(scope.row)"
              >删除</span
            >
            <span
              @click="handleEndingActive(scope.row)"
              class="action-wrap__btn"
              v-if="scope.row.state == 'ONGOING'"
              style="color: red"
              >结束</span
            >
            <span
              v-if="scope.row.state !== 'OVER'"
              class="action-wrap__btn"
              @click="oneCopy(scope.row)"
              >推广</span
            >
            <span class="action-wrap__btn" @click="showAround(scope.row)"
              >一键复制</span
            >
            <span
              v-if="scope.row.state == 'OVER'"
              class="action-wrap__btn"
              @click="viewsData(scope.row)"
              >查看数据</span
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <oneCopyDialog ref="oneCopy" :formInline="oneCopyData"></oneCopyDialog>
    <showRulesDialog :formInline="rules"></showRulesDialog>
    <pageCom
      :searchParams="searchParams"
      :total="total"
      @searchList="searchList"
    ></pageCom>
  </div>
</template>
<script>
import { sysNormalDisable } from '@utils/dictionariesFunc'
import {
  activeLists,
  endingActive,
  deleteActive,
  getCommUrl
} from '@/api/activeList/index'
import { filterImageMixins } from '@/mixins/filterImage'
import { saveScrollMixins } from '@/mixins/saveScroll'
import listImgString from '@common/listImgString' // 视图预览(返回值为字符串结构)-公共组件
import showRulesDialog from '@component/markingActive/common/showRulesDialog'
import oneCopyDialog from '@component/markingActive/common/oneCopyDialog' // 上部搜索条件
import topSearch from '@component/markingActive/invitedActivities/topSearch' // 上部搜索条件

export default {
  name: 'invitedActivities',
  msg: '邀请活动管理',
  mixins: [filterImageMixins, saveScrollMixins],
  components: {
    topSearch,
    oneCopyDialog,
    showRulesDialog,
    listImgString
  },
  data() {
    return {
      rules: {
        show: false,
        rulesData: ''
      },
      oneCopyData: {
        activeId: '',
        show: false,
        url: ''
      },
      typeLists: [],
      collegeList: [],
      srcList: [],
      dataList: [],
      tableData: [],
      total: 0,
      searchParams: {
        pageNum: 1, // 当前页数
        pageSize: 10 // 页面显示条数
      },
      formInline: {
        time: [],
        name: '',
        collegeId: '',
        createdTimeStart: '',
        createdTimeEnd: '',
        specificActivityType: 'INVITE_ACTIVITY',
        state: ''
      }
    }
  },
  mounted() {
    this.searchList()
    this.getColleges()
    this.getActivityTypes()
  },
  activated() {
    this.searchList()
  },
  methods: {
    showRules(val) {
      this.rules.rulesData = val.lotteryActivityConfig.ruleDescribe
      this.rules.show = true
    },
    showAround(val) {
      this.$router.push({
        name: 'wh:addInvitedActives',
        query: { type: 'copy', id: val.id }
      })
    },
    complie(val) {
      this.$router.push({
        name: 'wh:addInvitedActives',
        query: { type: 'complie', id: val.id }
      })
    },
    initUrl(val) {
      return new Promise((resolve, reject) => {
        let obj = {
          configKey: 'marketing_activity_url'
        }
        getCommUrl(
          obj,
          (res) => {
            this.oneCopyData.url = res + '/invite-activity?activityId=' + val.id
            resolve(this.oneCopyData.url)
          },
          (err) => {
            reject(err)
          }
        )
      })
    },

    async oneCopy(val) {
      this.oneCopyData.show = true
      await this.initUrl(val)
      this.$refs['oneCopy'].getQRcode(this.oneCopyData.url).then((img) => {
        this.$refs['oneCopy'].getUrl = img
      })
    },
    handleDeleteActive(val) {
      let _this = this
      this.$confirm('是否删除该活动?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteActive(
          val.id,
          {},
          () => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            _this.searchList()
          },
          (err) => {
            this.$message({
              type: 'warning',
              message: err.message
            })
          }
        )
      })
    },
    handleEndingActive(val) {
      let _this = this
      endingActive(
        val.id,
        {},
        () => {
          _this.searchList()
          _this.$message.warning('该活动已结束')
        },
        (err) => {
          _this.$message({
            message: err.message,
            type: 'success'
          })
        }
      )
    },
    async getActivityTypes() {
      let data = await sysNormalDisable(
        'specific_marketing_activity_type',
        'specific_marketing_activity_type'
      )
      this.typeLists = data
    },
    async getColleges() {
      let data = await sysNormalDisable('sys_college', 'sys_college')
      this.collegeList = data
    },
    // 查看数据
    viewsData(value) {
      this.$router.push({
        name: 'wh:viewDataDetails',
        query: {
          id: value.id,
          time: `${value.activityInfo.startTime}-${value.activityInfo.endTime}`,
          name: value.activityInfo.name
        }
      })
    },
    // 添加活动
    addActives() {
      this.$router.push({
        name: 'wh:addInvitedActives'
      })
    },
    toSearch() {
      this.searchParams.pageNum = 1
      this.searchList()
    },
    /* 换页 */
    changePage(page) {
      this.searchParams.pageNum = page
      this.searchList()
    },
    /* 获取列表 */
    searchList() {
      let _this = this
      let params = { ...this.searchParams, ...this.formInline }
      params.activityType = 'INVITE_ACTIVITY'
      activeLists(
        params,
        (res) => {
          res.rows.forEach((item) => {
            if (item.activityInfo.type == 'INVITE_ACTIVITY') {
              _this.srcList.push(item.inviteActivityConfig.poster)
            }
            _this.collegeList.forEach((one) => {
              if (item.activityInfo.collegeId == '') {
                item.activityInfo.collegeId = '全部学院'
              }
              if (item.activityInfo.collegeId == one.dictValue) {
                item.activityInfo.collegeId = one.dictLabel
              }
            })
            _this.typeLists.forEach((two) => {
              if (item.activityInfo.specificActivityType == two.dictValue) {
                item.activityInfo.specificActivityType = two.dictLabel
              }
            })
          })
          this.tableData = res.rows
          this.total = res.total
        },
        (err) => {}
      )
    },
    refreshList() {
      this.searchParams.pageNum = 1
      this.searchList()
    }
  }
}
</script>
<style lang="less" scoped>
.action-wrap {
  display: flex;
  justify-content: space-around;
  align-items: center;
  color: #02a7f0;
}
.action-wrap__btn {
  cursor: pointer;
}
.activity-time__wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
}
#invitedActivity {
  .data-input .el-input__inner {
    padding-left: 30px !important;
  }
  .img-box {
    position: relative;
    display: inline-block;
    width: 60px;
    height: 60px;
    border: 1px solid #ddd;

    .img-inner-box {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 100%;
      height: 100%;
      text-align: center;

      .el-image {
        width: 100%;
        height: 100%;
      }

      img {
        max-width: 100%;
        max-height: 100%;
        cursor: pointer;
      }
    }

    .num {
      position: absolute;
      display: block;
      bottom: 0;
      right: 0;
      width: 16px;
      height: 16px;
      background: rgba(0, 0, 0, 0.7);
      color: #fff;
      font-size: 12px;
      text-align: center;
      line-height: 16px;
    }
  }
}
</style>
